package com.emapp.lccode.view

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.viewbinding.ViewBinding
import com.emapp.lccode.adapter.DialogTypeMapAdapter
import com.emapp.lccode.base.dismissLoadingExt
import com.emapp.lccode.bean.ProblemTypeKt
import com.emapp.lccode.bean.SelectType
import com.emapp.lccode.databinding.PopTreetypeBinding
import com.emapp.lccode.mode.MapViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.vertical
import com.hjq.toast.Toaster


/**
 * 类型
 */
class MapDeviceTypeDialog : BaseDialog1(), DialogTypeMapAdapter.RefreshListener {
    private lateinit var mViewBind: PopTreetypeBinding
    private val adapter1: DialogTypeMapAdapter by lazy { DialogTypeMapAdapter() }
    private lateinit var myDialogViewModel: MapViewModel
    lateinit var selectType: SelectType
    var listener: SelectTypeListener? = null
    fun show(activity: FragmentActivity) {
        show(activity.supportFragmentManager, null)
    }

    var work_type1 = "3,6"
    var device_type1 = ""
    var isFirst = true
    override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): ViewBinding {
        return PopTreetypeBinding.inflate(inflater, container, false).also { mViewBind = it }
    }

    override fun onStart() {
        super.onStart()
        requireContext()
        myDialogViewModel =
            ViewModelProvider(this)[MapViewModel::class.java]
        myDialogViewModel.run {
            typesAll.observe(viewLifecycleOwner) { resultState ->
                if (!isFirst)
                    return@observe
                isFirst=false
                dismissLoadingExt()
                val l = arrayListOf<ProblemTypeKt>()
                l.addAll(resultState)
                l.forEach {
                    if (it.pid == "3" || it.pid == "6")
                        it.isCheck = true
                }
                setData(l)
            }
        }
        adapter1.listener = this

        mViewBind.rvList.run {
            vertical()
            adapter = adapter1
        }
        if (isFirst) {
            myDialogViewModel.getDeviceType("0")
        } else {
            val  l=MmkvUtil.getTypes()
            l?.forEach {
                it.isCheck=false
            }
            setData(l)
        }
        mViewBind.ivCancel.setOnClickListener {
            dismiss()
        }
        mViewBind.tvOk.setOnClickListener {
            var data = adapter1.items
            val w = data.filter { it.isCheck }
            val name = w.joinToString(",") { it.itemLabel }
            work_type1 = w.joinToString(",") { it.id }
            val d = arrayListOf<ProblemTypeKt>()
            w.forEach { l1 ->
                if (l1.child != null) {
                    val de1 = l1.child.filter { it.isCheck }
                    de1.forEach { l2 ->
                        val de2 = l2.child.filter { it.isCheck }
                        d.addAll(de2)
                    }
                    d.addAll(de1)
                }
            }

            device_type1 = d.joinToString(",") { it.id }
            if (device_type1.isEmpty()){
                Toaster.show("请选择分类")
                return@setOnClickListener
            }
            listener?.onSelectTypeComplete(work_type1, device_type1, name)
            dismiss()
        }
    }
    override fun initView() {


    }

    fun setData(resultState: ArrayList<ProblemTypeKt>?) {
        Log.e("TAG", "initView2: ------------------------------------------------------", )
        if (resultState == null) return
        var tt = work_type1
        if (device_type1 != "")
            tt = "$work_type1,$device_type1"
        Log.e("TAG", "setData: "+tt )
        val workTypeArray: List<String> =
            tt.split(",").map { it.toString() }.toList()
        resultState?.forEach { ty ->
            workTypeArray.forEach { id ->
                if (id == ty.id) {
                    ty.isCheck = true
                }
            }
        }
        val list = resultState.filter { it.pid == "0" }
        list.forEach { l ->
            val list2 = resultState.filter { it.pid == l.id }
            l.child = ArrayList(list2)
            list2.forEach { l2 ->
                val list3 = resultState.filter { it.pid == l2.id }
                l2.child = ArrayList(list3)
            }
        }
        adapter1.submitList(ArrayList(list))
        adapter1.notifyDataSetChanged()
    }

    interface SelectTypeListener {
        fun onSelectTypeComplete(work_type: String, device_type: String, work_type_name: String)
    }

    override fun onRefresh() {
        adapter1.notifyDataSetChanged()
    }

}